package juc.delayqueue;

import lombok.Builder;
import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Data
@Builder
public class OrderDelayed implements Delayed {

    /**
     * 订单Id
     */
    private String orderId;

    /**
     * 订单创建时间
     */
    private long createTime;

    /**
     * 订单支付过期时间
     */
    private long expireTime;

    /**
     * 判断延迟任务是否过期
     */
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expireTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    }
}
